<?php


namespace App\Services\WebSocket\SocketIO\Strategies;


use App\Services\WebSocket\SocketIO\Packet;

class HeartbeatStrategy
{
    public function handle($server,$frame) {
        $packet = $frame->data;
        $packetLength = strlen($packet);
        $payload ='';
        if (Packet::getPayload($packet)){
            return false;
        }
        if ($isPing = Packet::isSocketType($packet,'ping')){
            $payload.= Packet::PONG;
        }

        if ($isPing && $packetLength>1){
            $payload.= substr($packet,1,$packetLength-1);
        }
        if ($isPing) {
            $server ->push($frame->fd,$payload);
        }
        return true;
    }
}